﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace eLuk.CommandEngine
{
	public class IMAPCommand
	{
		public static int CommandSequence { get; set; }
		public string Identifier { get; set; }
		public CommandFormatter CommandFormatter { get; set; }
		public string Name { get; set; }
		public string Command { get; set; }
		public string[] CommonFailureStart { get; set; }
		public string ExecuteTemplate { get; set; }
		public string SuccessResponseStart { get; set; }
		public string FailureResponseStart { get; set; }
		public string ErrorResponseStart { get; set; }
		public IMAPCommand()
		{
		}

		public IMAPCommand( string _name, string _executeTemplate )
		{
			this.CommandFormatter = new CommandFormatter();
			this.Name = _name;
			this.ExecuteTemplate = _executeTemplate;
			//, , 
			this.SuccessResponseStart = "{0} OK ";//_successResponseStart;
			this.FailureResponseStart = "{0} NO ";//_failureResponseStart;
			this.ErrorResponseStart = "{0} BAD ";//_errorResponseStart;

			this.Identifier = "A" + ( ++IMAPCommand.CommandSequence ).ToString().PadLeft( 4, '0' );
			this.SuccessResponseStart = string.Format( this.SuccessResponseStart, this.Identifier );
			this.FailureResponseStart = string.Format( this.FailureResponseStart, this.Identifier );
			this.ErrorResponseStart = string.Format( this.ErrorResponseStart, this.Identifier );
			this.CommonFailureStart = new string[] { "* BAD ", "BAD ", " BAD " };

		}
		public byte[] Prepare( params object[] CommandParams )
		{
			string result = "";
			result = string.Format( this.ExecuteTemplate + "\r\n", CommandParams );
			result = this.Identifier + " " + result;
			this.Command = result;
			return Encoding.UTF8.GetBytes( result.ToCharArray() );
		}
	}

	public class IMAPCommands
	{
		public static int CommandSequence { get; set; }
		private IMAPCommand Capability { get; set; }
		private IMAPCommand Login { get; set; }
		private IMAPCommand Authenticate { get; set; }
		private IMAPCommand Store { get; set; }
		private IMAPCommand List { get; set; }
		private IMAPCommand XList { get; set; }
		private IMAPCommand Rename { get; set; }
		private IMAPCommand Delete { get; set; }
		private IMAPCommand Examine { get; set; }
		private IMAPCommand Expunge { get; set; }
		private IMAPCommand Create { get; set; }
		private IMAPCommand Search { get; set; }
		private IMAPCommand Copy { get; set; }
		private IMAPCommand Fetch { get; set; }
		private IMAPCommand Select { get; set; }
		private IMAPCommand SetMetaData { get; set; }
		private IMAPCommand GetQuotaRoot { get; set; }
		private IMAPCommand Append { get; set; }
		private IMAPCommand Compress { get; set; }
		private IMAPCommand Logout { get; set; }
		public IMAPCommands()
		{

		}
	}

}
